/*
 * ArCoLIVE - Live Internet Videoconference Environment
 * Information Technology Departament - Federal University of Alagoas - Brazil
 *
 * Copyright (C) 2005 Leandro Melo de Sales (leandro@labpesquisas.tci.ufal.br)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package br.ufal.tci.nexos.arcolive.service.connection;

/**
 * CLASSNAME.java
 *
 * CLASS DESCRIPTION
 *
 * @see CLASSNAME
 *
 * @author <a href="mailto:felipe@labpesquisas.tci.ufal.br">Felipe Barros Pontes</a>.
 * @author <a href="mailto:leandro@labpesquisas.tci.ufal.br">Leandro Melo de Sales</a>.
 * @since 0.1
 * @version 0.1
 *
 * <p><b>Revisions:</b>
 *
 * <p><b>yyyymmdd USERNAME:</b>
 * <ul>
 * <li> VERSION
 * </ul>
 */

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import br.ufal.tci.nexos.arcolive.exception.ArCoLIVECannotAddParticipantInGroupException;
import br.ufal.tci.nexos.arcolive.exception.ArCoLIVECannotStartServerSocketException;
import br.ufal.tci.nexos.arcolive.logger.ArCoLIVELogRepository;

public class ArCoLIVEConnectionManager extends Thread {

    private NewConnectionListenerIF listener;
    private boolean listeningForConnection = true;
    private int port = -1;
    private static ArCoLIVEConnectionManager singleton = null;
    private ServerSocket serverSocket = null;
    
    private ArCoLIVEConnectionManager() {

    }
    
    public static ArCoLIVEConnectionManager getInstance() {
    	if (singleton == null) {
    		singleton = new ArCoLIVEConnectionManager();
    	}
    	return singleton;
    }

    public void setListener(NewConnectionListenerIF listener) {
    	this.listener = listener;
    }
    
    public void run() {
    	try {
			
			while (this.listeningForConnection) {
				Socket clientSocket = this.serverSocket.accept();				
				this.listener.newConnectionRequest(clientSocket);
			}
			
		} catch (ArCoLIVECannotAddParticipantInGroupException alcapge) {
			// tem que enviar uma mensagem para o cliente dizendo que ele nao pode entrar
			ArCoLIVELogRepository.getInstance().log("O cliente n pôde entrar... tem que enviar uma mensagem para o cliente dizendo que ele nao pode entrar");
    	} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}
	
	public void startListen() throws ArCoLIVECannotStartServerSocketException {
		if (port == -1) {
			throw new ArCoLIVECannotStartServerSocketException("Listen port not set");
		}
		try {
			this.serverSocket = new ServerSocket(this.port);
			//this.serverSocket.bind(serverAddress);
			//this.serverSocket.setSocketFactory();
			//this.serverSocket.bind(new ArCoLIVESocketAddress("teste"));
		} catch (IOException e) {
			throw new ArCoLIVECannotStartServerSocketException("Error while trying to start server socket on port " + this.port + ": " + e.getMessage());
		}
		super.start();
	}
	
	public InetAddress getListenInetAddress() {
		return this.serverSocket.getInetAddress();
	}
	
}


